<?php
/**
 * Created by PhpStorm.
 * User: liuchaozhong
 * Date: 2018/8/18
 * Time: 下午2:45
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Checkin extends Model
{
    protected $table = 'checkin';

    /**
     * 判断是否连续签到
     * @param $member_id 用户id
     * @param $today 当天日期
     */
    public function isLx($member_id, $today)
    {
        $next_info  = Checkin::where('member_id',$member_id)->where('next_date', $today)->first();

        return !empty($next_info) ? true : false;
    }

    /**
     * 判断是否已经签到
     * @param $member_id  用户id
     * @param $today  当天日期
     */
    public function isCheck($member_id, $today)
    {
        $check = Checkin::where('member_id', $member_id)->where('check_date', $today)->first();

        return !empty($check) ? true : false;
    }

    /**
     * 获取用户签到金额
     * @param $member_id 用户id
     * @param $is_lx 是否连续签到
     */
    public function getCheckMoney($member_id, $is_lx = true)
    {
        $member_info = Member::find($member_id);
        $check_day = $member_info->check_day;
        $check_task = TaskConfig::where('name','7天连续签到')->first(); //写死,默认签到任务
        $task_config = unserialize($check_task['config']);
        if($is_lx){
            $money = preg_replace('/\D+/', '', $task_config['td'][$check_day]);
        }else{
            $money = preg_replace('/\D+/', '', $task_config['td'][0]);
            $check_day = 0;
        }
        $result = [
            'money' => $money,                 //签到天数对应的奖励
            'day' => $check_day,               //签到天
            'no' => count($task_config['td'])  //总共多少设置了天
        ];

        return $result;
    }

}
